package com.gcd.skymallorderitemapi.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.model.Cart;
import com.situ.model.OrderItem;
import com.situ.service.OrderItemService;
import com.situ.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping(value = "/api/v1/orderItem", produces = MediaType.APPLICATION_JSON_VALUE)
public class OrderItemApi {
    private OrderItemService orderItemService;

    @Autowired
    public void setOrderItemService(OrderItemService orderItemService) {
        this.orderItemService = orderItemService;
    }

    //查询所有
    @GetMapping
    ResponseEntity<JsonResult<?>> findAll(@RequestParam(defaultValue = "1") Integer pageNo,
                                          @RequestParam(defaultValue = "10") Integer pageSize,
                                          OrderItem orderItem) {
        Page<OrderItem> page = new Page<>(pageNo, pageSize);
        page = this.orderItemService.findAll(page, orderItem);
        return ResponseEntity.ok(JsonResult.success(page));
    }

    //根据订单id查询
    @GetMapping("/orderId/{orderId}")
    ResponseEntity<JsonResult<?>> findByOrderId(@RequestParam(defaultValue = "1") Integer pageNo,
                                                @RequestParam(defaultValue = "10") Integer pageSize,
                                                @PathVariable("orderId") Integer orderId) {
        Page<OrderItem> page = new Page<>(pageNo, pageSize);
        page = this.orderItemService.findByOrderId(page, orderId);
        return ResponseEntity.ok(JsonResult.success(page));
    }

    //删除订单项
    @DeleteMapping
    ResponseEntity<JsonResult<?>> deleteOrderItem(@RequestBody Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        int result = this.orderItemService.deleteOrderItem(list);
        if (result == 0) {
            return ResponseEntity.ok(JsonResult.fail("删除失败"));
        } else {
            return ResponseEntity.ok(JsonResult.success(result));
        }
    }

    //修改订单项
    @PutMapping
    ResponseEntity<JsonResult<?>> updateOrderItem(@RequestBody OrderItem orderItem) {
        boolean result = this.orderItemService.updateOrderItem(orderItem);
        if (result) {
            return ResponseEntity.ok(JsonResult.success(result));
        } else {
            return ResponseEntity.ok(JsonResult.fail("修改失败"));
        }
    }

    //todo添加订单项
    @PostMapping
    ResponseEntity<JsonResult<?>> createOrderItem(@RequestBody List<Cart> carts) {
        boolean result = this.orderItemService.createOrderItem(carts);
        if (result) {
            return ResponseEntity.ok(JsonResult.success(result));
        } else {
            return ResponseEntity.ok(JsonResult.fail("添加失败"));
        }
    }

    //添加秒杀订单项
    @PostMapping("/create_seckill_orderItem")
    ResponseEntity<JsonResult<?>> createSeckillOrderItem(@RequestBody OrderItem orderItem) {
        boolean success= orderItemService.createSeckillOrderItem(orderItem);
        if(success) {
            return ResponseEntity.ok(JsonResult.success(true));
        } else {
            return ResponseEntity.ok(JsonResult.fail("插入订单项失败"));
        }
    }
}
